<template>
  <div class="about">
    <div class="container">
      <div style="margin: 20px 0">
        <el-input placeholder="请输入水果名称" v-model="name" style="width: 200px;margin-right: 20px"></el-input>
        <el-input placeholder="请输入水果类型" v-model="category" style="width: 200px;margin-right: 20px"></el-input>
        <el-button type="primary" @click="load(pageNum)">查询</el-button>
        <el-button type="warning" @click="returnInfo">重置</el-button>
      </div>

      <div style="margin: 20px 0">
        <el-button type="primary" @click="FormAddInfoAn" plain>添加数据</el-button>
        <el-button type="danger" @click="deleteBatch">批量删除</el-button>
      </div>

      <el-table :data="tableData" @selection-change="handleSelectionChange" style="border-radius: 10px"
                :header-cell-style="{background:'aliceblue',height:'45px', color:'#666'}">
        <el-table-column type="selection" align="center" width="55"></el-table-column>
        <el-table-column align="center" prop="userid" label="用户ID"></el-table-column>
        <el-table-column align="center" prop="no" show-overflow-tooltip label="订单编号"></el-table-column>
        <el-table-column align="center" prop="name" label="订单名称"></el-table-column>
        <el-table-column align="center" prop="money" label="订单金额"></el-table-column>
        <el-table-column align="center" prop="category" label="订单类型"></el-table-column>
        <el-table-column align="center" prop="date" label="订单日期"></el-table-column>
        <el-table-column align="center" width="170px" label="操作">
          <template v-slot="scope" style="width: 340px">
            <el-button style="width: 65px; margin-right: 10px" type="success" plain @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button style="width: 65px;" type="danger" plain @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin: 20px 0">
        <el-pagination
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>

      <el-dialog title="上架水果" :visible.sync="FormAddInfo" width="40%">
        <el-form label-width="80px" style="padding-right: 30px" ref="formRef" :rules="rules" :model="users"
                 class="demo-form-inline">
          <el-form-item prop="title" style="margin-top: 30px;" label="订单名称">
            <el-input v-model="users.name"></el-input>
          </el-form-item>

          <el-form-item prop="title" style="margin-top: 30px;" label="订单金额">
            <el-input v-model="users.money"></el-input>
          </el-form-item>

          <el-form-item label="订单分类"prop="content">
            <el-select style="width: 100%" v-model="users.category">
              <el-option v-for="item in ['水果','蔬菜','零食','饮料','奶制品','糕点']" :key="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" style="text-align: center; margin-top: -10px">
          <el-button @click="FormAddInfo = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>

      </el-dialog>

      <el-dialog title="水果订单" :visible.sync="FormUpdateInfo" width="40%" @close="onClose01">
        <el-form label-width="80px" style="padding-right: 30px" ref="formRef" :rules="rules" :model="forms"
                 class="demo-form-inline">

          <el-form-item prop="title" style="margin-top: 30px;" label="订单名称">
            <el-input v-model="forms.name"></el-input>
          </el-form-item>

          <el-form-item prop="title" style="margin-top: 30px;" label="订单金额">
            <el-input v-model="forms.money"></el-input>
          </el-form-item>

          <el-form-item label="订单分类"prop="content">
            <el-select style="width: 100%" v-model="forms.category">
              <el-option v-for="item in ['水果','蔬菜','零食','饮料','奶制品','糕点']" :key="item" :value="item"></el-option>
            </el-select>
          </el-form-item>

        </el-form>

        <div slot="footer" style="text-align: center; margin-top: -10px">
          <el-button @click="FormUpdateInfo = false">取 消</el-button>
          <el-button type="primary" @click="onUpdate">确 定</el-button>
        </div>

      </el-dialog>

      <el-dialog title="新闻详情" :visible.sync="NewsInfo" width="40%" >
        <div class="w-e-text">
          <div v-html="content"></div>
        </div>

        <div slot="footer" style="text-align: center; margin-top: -10px">
          <el-button type="primary" plain @click="NewsInfo = false">确 定</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import request from "@/utils/request"

export default {
  data() {
    return {
      Info: JSON.parse(localStorage.getItem('user11') || '{}'),
      pageSize: 5,
      pageNum: 1,
      tableData: [],
      category: '',
      total: 0,
      FormAddInfo: false,
      NewsInfo: false,
      user: JSON.parse(localStorage.getItem('user11') || '{}'),
      users: {},
      name: '',
      money: '',
      ids: [],
      forms: {},
      FormUpdateInfo: false,

      rules: {
        name: [
          {required: true, message: '请输入水果名称', trigger: 'blur'},
        ],
        money: [
          {required: true, message: '请输入水果价格', trigger: 'blur'},
        ],
        category: [
          {required: true, message: '请输入类型', trigger: 'blur'},
        ]
      }
    }
  },
  created() {
    this.load()
  },
  methods: {

    showContent(content) {
      this.NewsInfo = true
      this.content = content
    },

    handleSwitchChange(from) {
      //深拷贝一下，以防止页面渲染成功而数据库未修改成功
      this.forms = JSON.parse(JSON.stringify(from))
      //触发更新
      request.post('/orders/update', this.forms).then(res => {
        if (res.code === 200) {
          this.$message.success('状态修改成功')
          //  浏览器页面自动更新
          this.FormUpdateInfo = false
          this.load()
        } else {
          // 登录失败
          this.$message.error(res.message)
        }
      })
    },

    //批量删除
    deleteBatch() {
      if(this.ids.length === 0) {
        this.$message.warning('请选择要批量删除的数据')
        return
      }
      this.$confirm('确定批量删除吗？', '提示', {type: 'warning'}).then(() => {
        request.delete('/orders/deleteBatch', {
          data:this.ids
        }).then(res => {
          if (res.code === 200) {
            this.$message.success('批量删除成功')
            this.load(1)
          } else {
            // 登录失败
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消批量删除'});
      })
    },

    //批量删除获取数据
    handleSelectionChange(rows) {
      // 提取所有选中的 id 并拼接成字符串
      this.ids = rows.map(v => v.id);
    },

    FormAddInfoAn() {
      this.FormAddInfo = true,
          this.users = {}
    },

    //删除单条数据
    handleDelete(id) {
      this.$confirm('确定删除吗？', '提示', {type: 'warning'}).then(() => {
        request.delete('/orders/delete/' + id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.load(1)
          } else {
            // 登录失败
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消删除'});
      })
    },

    //编辑数据
    handleEdit(row) {
      this.FormUpdateInfo = true,
          this.forms = JSON.parse(JSON.stringify(row))
    },
    //确认是否确认提交修改
    onUpdate() {
      this.$confirm('确定用户信息吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //触发更新
        request.post('/orders/update', this.forms).then(res => {
          if (res.code === 200) {
            this.$message.success('修改成功')
            //  浏览器页面自动更新
            this.FormUpdateInfo = false
            this.load()
          } else {
            // 登录失败
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消修改'});
      })
    },

    returnInfo() {
      this.name = '',
      this.category = '',
      this.load()
    },
    load(pageNum) {
      this.pageNum = pageNum || 1
      request.get('/orders/selectByPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          category: this.category,
        }
      }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },

    //  添加表单
    onSubmit() {
      //获取编辑框信息
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$confirm('再次确定添加用户信息？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //触发更新
            request.post('/orders/add', this.users).then(res => {
              if (res.code === 200) {
                this.$message.success('添加成功')
                //  浏览器页面自动更新
                this.FormAddInfo = false
                this.load()
              } else {
                // 登录失败
                this.$message.error(res.message)
              }
            })
          }).catch(() => {
            this.$message({type: 'info', message: '已取消修改'});
          })
        }
      })
    },
  }
}
;
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.el-tooltip__popper{
  max-width:200px !important;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
  border-radius: 50%;
  margin-top: -10px;
  margin-bottom: 10px;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
